<?php
namespace  App\Librarys;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class Mylog
{
    public static $logger = null;

    /**
     * 调用实例 MyLog::info('apis_cloud_subjects', $msg);
     * $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
     * $logfile = strtolower(str_replace('\\', '_', $trace[1]['class'])) . '_' . strtolower($trace[1]['function']);
     * MyLog::info($logfile, ['url' => $url, 'method' => $method, 'req' => $data, 'res' => $res, ]);
     * @param $func
     * @param $args
     * User: aniu
     * Date: 2020/6/14 6:11 下午
     */
    public static function __callStatic($func, $args)
    {
        if (!self::$logger){
            self::$logger = new Logger('');
        }
        @list($name, $msg, $day) = $args;
        if (is_array($msg)) {
            $msg = json_encode($msg, JSON_PRETTY_PRINT);
        }
        $path = storage_path() . '/logs/' . $name . '.log';

        $handler = new RotatingFileHandler($path, $day ? $day : 10);
        $handler->setFormatter(new LineFormatter(null, null, true, true));

        self::$logger->pushHandler($handler);
        self::$logger->$func($msg);
    }
}
